package com.example.flutter_android_tv

import android.annotation.TargetApi
import android.app.UiModeManager
import android.content.Context
import android.content.Context.UI_MODE_SERVICE
import android.content.Intent
import android.content.IntentFilter
import android.content.res.Configuration
import android.os.BatteryManager
import android.os.Build
import android.telephony.TelephonyManager
import kotlin.math.pow
import kotlin.math.sqrt

class DeviceInfoUtil {
    companion object{

        /*对外 是否TV*/
        fun isTV(context: Context) : Boolean {
            return checkIsTelevision(context)
        }

        /*
        * 判断屏幕是否大于20寸
        * */
        private fun checkScreenInfo(context: Context) : Boolean {
            val metrics = context.resources.displayMetrics;
            val width = (metrics.widthPixels / metrics.xdpi).toDouble().pow(2.0);
            val height = (metrics.heightPixels / metrics.ydpi).toDouble().pow(2.0)

            val size = sqrt(width+height)

            return size > 20;
        }

        /*判断是否有sim卡*/
        private fun checkSIMCard(context: Context) : Boolean {
            val manager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager;
            return  manager.phoneType == TelephonyManager.PHONE_TYPE_NONE;
        }


        /*判断设备是否满电 && 交流电*/
        @TargetApi(Build.VERSION_CODES.ECLAIR)
        private fun checkAlternatingCurrent(context: Context) : Boolean {
            val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            val batteryStatus: Intent? = context.registerReceiver(null,filter) ?: return false;
            //电池状态
            val status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            val isCharging = status == BatteryManager.BATTERY_STATUS_FULL;
            //充电状态
            val chargePlug = batteryStatus?.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);
            val isACCharging = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
            /// 电池是满的 并且是AC交流电 才认为是电视
            return isCharging && isACCharging;
        }

        /*判断是否是TV*/
        @TargetApi(Build.VERSION_CODES.FROYO)
        private fun checkIsTelevision(context: Context) : Boolean {
            val manager = context.getSystemService(UI_MODE_SERVICE) as UiModeManager;
            return manager.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION;
        }
    }
}